<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller {
	private $data = array();

	function __construct()
	{
		parent::__construct();
		$this->load->model('process_data');
		$this->load->model('sessionfunctions');


		$rs =  $this->process_data->get_job_role();
		$list_jobRole = array(0 => "Select your job role");
		foreach ($rs as $val){
			$list_jobRole[$val['id']] = $val['name'];
		}
		$this->data['list_jobRole'] = $list_jobRole;


		$rs =  $this->process_data->get_organization_type();
		$list_organizationType = array(0 => "Select your organization type");
		foreach ($rs as $val){
			$list_organizationType[$val['id']] = $val['name'];
		}
		$this->data['list_organizationType'] = $list_organizationType;



		$rs =  $this->process_data->get_area_of_interest();
		$list_areaOfInterest = array();
		foreach ($rs as $val){
			$list_areaOfInterest[$val['id']] = $val['name'];
		}
		$this->data['list_areaOfInterest'] = $list_areaOfInterest;



		$rs =  $this->process_data->get_det_country();
		$list_country = array();
		foreach ($rs as $val){
			$list_country[$val['id']] = $val['name'];
		}
		$this->data['list_country'] = $list_country;

	}


	public function index()
	{
		$this->newuser();
	}

	public function newuser($doNext = "register")
	{
		if(count($_POST) > 0){
			//Insert into db for user registration
			//Check for email is existing
			if($this->process_data->isEmailExisting($this->input->post('email')) == false){
				$userId = $this->process_data->user_registration_step();
				//Set the email id and user type in the session
				$this->sessionfunctions->setSessionData($this->input->post('email'), $userId, 'user');

				if($this->input->post('doNext') != "register"){
					//Redirect for uploading journal
					redirect(base_url().'/journal/publishnew/');
				}else{
					redirect(base_url().'/home/index/');
				}
			}
		}

		$data = $this->data;
		$data['doNext'] = $doNext;
		$this->load->view('register', $data);
	}
	
	public function updateuser()	{
		$userId = $this->process_data->user_registration_step();
		redirect(base_url()."myprofile/mysettings");
	}
	
	public function changepassword()	{
		$userId = $this->process_data->changePassword();
		$data['msg'] = "Password Changed Successfully";
		redirect(base_url()."myprofile/mysettings");
	}
	
	public function forgotpassword(){
		$this->load->view('forgotpassword');
	}
	public function login(){
		//Check for email id and password
		$formLoginError = "";
		if(count($_POST) > 0){
			$rs = $this->process_data->matchAndGetUserDetail($this->input->post('email_id'), $this->input->post('password'));
			if(count($rs)>0){
				$this->sessionfunctions->setSessionData($this->input->post('email_id'), $rs[0]['id'], 'user');
				redirect(base_url().'myprofile/index/');
			}else{
				$formLoginError = "Error";
			}
		}
		$data['formLoginError'] = $formLoginError;
		//If failed to login show Errors
		$this->load->view('login', $data);
	}

	public function newsletter(){
		$this->load->view('newsletter');
	}
	
	public function enquirey(){
		$this->process_data->enquirey();
		redirect(base_url().'contactus?msg=success');
	}

}
